
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>抽象数据类型队列的定义和实现 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="栈.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    数据结构（C语言版） 复习总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1.html">
            
                <a href="../chapter1.html">
            
                    
                    第一章 绪论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter2/chapter2.html">
            
                <a href="../chapter2/chapter2.html">
            
                    
                    第二章 线性表
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/顺序表.html">
            
                <a href="../chapter2/顺序表.html">
            
                    
                    线性表的顺序表示和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/链表.html">
            
                <a href="../chapter2/链表.html">
            
                    
                    线性表的链式表示和实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter3.html">
            
                <a href="chapter3.html">
            
                    
                    第三章 栈和队列
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="栈.html">
            
                <a href="栈.html">
            
                    
                    抽象数据类型栈的定义和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.2" data-path="队列.html">
            
                <a href="队列.html">
            
                    
                    抽象数据类型队列的定义和实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    第四章 串
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter4/串类型.html">
            
                <a href="../chapter4/串类型.html">
            
                    
                    串类型的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter4/串的表示和实现.html">
            
                <a href="../chapter4/串的表示和实现.html">
            
                    
                    串的表示和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter4/串的模式匹配算法.html">
            
                <a href="../chapter4/串的模式匹配算法.html">
            
                    
                    串的模式匹配算法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    第五章 数组和广义表
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/数组的定义.html">
            
                <a href="../chapter5/数组的定义.html">
            
                    
                    数组的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/矩阵的压缩存储.html">
            
                <a href="../chapter5/矩阵的压缩存储.html">
            
                    
                    矩阵的压缩存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter5/广义表.html">
            
                <a href="../chapter5/广义表.html">
            
                    
                    广义表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    第六章 树和二叉树
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter6/树.html">
            
                <a href="../chapter6/树.html">
            
                    
                    树的定义与基本术语
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/二叉树.html">
            
                <a href="../chapter6/二叉树.html">
            
                    
                    二叉树
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.2.1" data-path="../chapter6/二叉树的二叉链表实现.html">
            
                <a href="../chapter6/二叉树的二叉链表实现.html">
            
                    
                    二叉树的二叉链表实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter6/遍历二叉树和线索二叉树.html">
            
                <a href="../chapter6/遍历二叉树和线索二叉树.html">
            
                    
                    遍历二叉树和线索二叉树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter6/树和森林.html">
            
                <a href="../chapter6/树和森林.html">
            
                    
                    树和森林
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter6/赫夫曼树.html">
            
                <a href="../chapter6/赫夫曼树.html">
            
                    
                    赫夫曼树
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" >
            
                <span>
            
                    
                    第七章 图
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter7/图.html">
            
                <a href="../chapter7/图.html">
            
                    
                    图的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter7/图的存储结构.html">
            
                <a href="../chapter7/图的存储结构.html">
            
                    
                    图的存储结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter7/图的遍历.html">
            
                <a href="../chapter7/图的遍历.html">
            
                    
                    图的遍历
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter7/最小生成树.html">
            
                <a href="../chapter7/最小生成树.html">
            
                    
                    最小生成树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter7/有向无环图.html">
            
                <a href="../chapter7/有向无环图.html">
            
                    
                    有向无环图的应用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter8/查找.html">
            
                <a href="../chapter8/查找.html">
            
                    
                    第八章 查找
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter8/静态查找表.html">
            
                <a href="../chapter8/静态查找表.html">
            
                    
                    静态查找表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter8/动态查找表.html">
            
                <a href="../chapter8/动态查找表.html">
            
                    
                    动态查找表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter8/哈希表.html">
            
                <a href="../chapter8/哈希表.html">
            
                    
                    哈希表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter9/内部排序.html">
            
                <a href="../chapter9/内部排序.html">
            
                    
                    第九章 内部排序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter9/插入排序.html">
            
                <a href="../chapter9/插入排序.html">
            
                    
                    插入排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter9/选择排序.html">
            
                <a href="../chapter9/选择排序.html">
            
                    
                    选择排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter9/快速排序.html">
            
                <a href="../chapter9/快速排序.html">
            
                    
                    快速排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter9/归并排序和基数排序.html">
            
                <a href="../chapter9/归并排序和基数排序.html">
            
                    
                    归并排序和基数排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter9/排序方法的比较.html">
            
                <a href="../chapter9/排序方法的比较.html">
            
                    
                    排序方法的比较
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >抽象数据类型队列的定义和实现</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x62BD;&#x8C61;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x961F;&#x5217;&#x7684;&#x5B9A;&#x4E49;&#x548C;&#x5B9E;&#x73B0;">&#x62BD;&#x8C61;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x961F;&#x5217;&#x7684;&#x5B9A;&#x4E49;&#x548C;&#x5B9E;&#x73B0;</h2>
<p><strong>&#x961F;&#x5217;</strong>&#x662F;&#x4E00;&#x79CD;<strong>&#x5148;&#x8FDB;&#x5148;&#x51FA;</strong>&#xFF08;<code>FIFO</code>&#xFF09;&#x7684;&#x7EBF;&#x6027;&#x8868;&#xFF0C;&#x5B83;&#x53EA;&#x5141;&#x8BB8;&#x5728;&#x8868;&#x7684;&#x4E00;&#x7AEF;&#x8FDB;&#x884C;&#x63D2;&#x5165;&#xFF0C;&#x5728;&#x53E6;&#x4E00;&#x7AEF;&#x5220;&#x9664;&#x5143;&#x7D20;&#x3002;</p>
<ul>
<li>&#x5141;&#x8BB8;&#x63D2;&#x5165;&#x7684;&#x4E00;&#x7AEF;&#x53EB;&#x505A;<strong>&#x961F;&#x5C3E;</strong></li>
<li>&#x5141;&#x8BB8;&#x5220;&#x9664;&#x7684;&#x4E00;&#x7AEF;&#x79F0;&#x4E3A;<strong>&#x961F;&#x5934;</strong></li>
</ul>
<blockquote>
<p>&#x9664;&#x4E86;&#x6808;&#x548C;&#x961F;&#x5217;&#x4E4B;&#x5916;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x79CD;&#x9650;&#x5B9A;&#x6027;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x662F;<strong>&#x53CC;&#x7AEF;&#x961F;&#x5217;</strong>&#xFF0C;&#x5B83;&#x662F;&#x9650;&#x5B9A;&#x63D2;&#x5165;&#x548C;&#x5220;&#x9664;&#x64CD;&#x4F5C;&#x5728;&#x8868;&#x7684;&#x4E24;&#x7AEF;&#x8FDB;&#x884C;&#x7684;&#x7EBF;&#x6027;&#x8868;&#x3002;&#x5728;&#x5B9E;&#x9645;&#x4F7F;&#x7528;&#x4E2D;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x6709;&#x8F93;&#x51FA;&#x53D7;&#x9650;&#x7684;&#x53CC;&#x7AEF;&#x961F;&#x5217;&#xFF08;&#x5373;&#x4E00;&#x4E2A;&#x7AEF;&#x70B9;&#x5141;&#x8BB8;&#x63D2;&#x5165;&#x548C;&#x5220;&#x9664;&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x7AEF;&#x70B9;&#x53EA;&#x5141;&#x8BB8;&#x63D2;&#x5165;&#xFF09;&#x548C;&#x8F93;&#x5165;&#x53D7;&#x9650;&#x7684;&#x53CC;&#x7AEF;&#x961F;&#x5217;&#xFF08;&#x5373;&#x4E00;&#x4E2A;&#x7AEF;&#x70B9;&#x5141;&#x8BB8;&#x63D2;&#x5165;&#x548C;&#x5220;&#x9664;&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x7AEF;&#x70B9;&#x53EA;&#x5141;&#x8BB8;&#x5220;&#x9664;&#xFF09;&#x3002;</p>
</blockquote>
<h3 id="&#x94FE;&#x961F;&#x5217;&#x7684;&#x8868;&#x793A;&#x548C;&#x5B9E;&#x73B0;">&#x94FE;&#x961F;&#x5217;&#x7684;&#x8868;&#x793A;&#x548C;&#x5B9E;&#x73B0;</h3>
<p>&#x7528;&#x94FE;&#x8868;&#x8868;&#x793A;&#x7684;&#x961F;&#x5217;&#x7B80;&#x79F0;&#x4E3A;<strong>&#x94FE;&#x961F;&#x5217;</strong>&#x3002;&#x4E00;&#x4E2A;&#x94FE;&#x961F;&#x5217;&#x9700;&#x8981;&#x4E24;&#x4E2A;&#x5206;&#x522B;&#x6307;&#x793A;&#x961F;&#x5934;&#x548C;&#x961F;&#x5C3E;&#x7684;&#x6307;&#x9488;&#xFF08;&#x5206;&#x522B;&#x79F0;&#x4E3A;&#x5934;&#x6307;&#x9488;&#x548C;&#x5C3E;&#x6307;&#x9488;&#xFF09;&#x624D;&#x80FD;&#x60DF;&#x4E00;&#x786E;&#x5B9A;&#x3002;&#x6211;&#x4EEC;&#x4E5F;&#x7ED9;&#x94FE;&#x961F;&#x5217;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5934;&#x7ED3;&#x70B9;&#xFF0C;&#x5E76;&#x4EE4;&#x5934;&#x6307;&#x9488;&#x6307;&#x5411;&#x5934;&#x7ED3;&#x70B9;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x7A7A;&#x7684;&#x94FE;&#x961F;&#x5217;&#x7684;&#x5224;&#x65AD;&#x6761;&#x4EF6;&#x4E3A;&#x5934;&#x6307;&#x9488;&#x548C;&#x5C3E;&#x6307;&#x9488;&#x5747;&#x6307;&#x5411;&#x5934;&#x7ED3;&#x70B9;&#x3002;</p>
<h4 id="&#x5355;&#x94FE;&#x961F;&#x5217;">&#x5355;&#x94FE;&#x961F;&#x5217;</h4>
<pre><code class="lang-C"><span class="hljs-comment">/**
 * &#x5B9A;&#x4E49;&#x5355;&#x94FE;&#x961F;&#x5217;&#x7ED3;&#x70B9;&#x7ED3;&#x6784;
 */</span>
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> QNode
{
    ElemType data;
    <span class="hljs-keyword">struct</span> QNode *next;
}QNode, *QueuePtr;

<span class="hljs-comment">/**
 * &#x5B9A;&#x4E49;&#x94FE;&#x961F;&#x5217;
 */</span>
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span>
{
    QueuePtr front; <span class="hljs-comment">// &#x961F;&#x5934;&#x6307;&#x9488;</span>
    QueuePtr rear; <span class="hljs-comment">// &#x961F;&#x5C3E;&#x6307;&#x9488;</span>
}LinkQueue;

<span class="hljs-comment">/**
 * &#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E2A;&#x7A7A;&#x961F;&#x5217;
 */</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">initQueue</span><span class="hljs-params">(LinkQueue &amp;Q)</span>
</span>{
    Q.front = Q.rear = (QueuePtr) <span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(QNode)); <span class="hljs-comment">// &#x4E3A;&#x5934;&#x7ED3;&#x70B9;&#x5206;&#x914D;&#x7A7A;&#x95F4;&#xFF0C;&#x5E76;&#x4F7F;&#x5934;&#x3001;&#x5C3E;&#x6307;&#x9488;&#x6307;&#x5411;&#x5934;&#x7ED3;&#x70B9;</span>
    <span class="hljs-keyword">if</span> (!Q.front) <span class="hljs-comment">// &#x82E5;&#x7A7A;&#x95F4;&#x5206;&#x914D;&#x5931;&#x8D25;</span>
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;ERROR\n&quot;</span>);
        <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
    }
    Q.front-&gt;next = <span class="hljs-literal">NULL</span>; <span class="hljs-comment">// &#x5934;&#x7ED3;&#x70B9;&#x6307;&#x5411; NULL</span>
}

<span class="hljs-comment">/**
 * &#x9500;&#x6BC1;&#x961F;&#x5217;
 */</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">DestoryQueue</span><span class="hljs-params">(LinkQueue &amp;Q)</span>
</span>{
    <span class="hljs-keyword">while</span> (Q.front)
    {
        Q.rear = Q.front-&gt;next;
        <span class="hljs-built_in">free</span>(Q.front);
        Q.front = Q.rear;
    }
}

<span class="hljs-comment">/**
 * &#x63D2;&#x5165; e &#x4F5C;&#x4E3A;&#x961F;&#x5C3E;&#x65B0;&#x5143;&#x7D20;
 */</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">enQueue</span><span class="hljs-params">(LinkQueue &amp;Q, ElemType e)</span>
</span>{
    QueuePtr p = (QueuePtr) <span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(QNode));
    <span class="hljs-keyword">if</span> (!p) <span class="hljs-comment">// &#x82E5;&#x7A7A;&#x95F4;&#x5206;&#x914D;&#x5931;&#x8D25;</span>
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;ERROR\n&quot;</span>);
        <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
    }
    p-&gt;data = e;
    p-&gt;next = <span class="hljs-literal">NULL</span>;
    Q.rear-&gt;next = p; <span class="hljs-comment">// &#x539F;&#x961F;&#x5C3E;&#x7ED3;&#x70B9;&#x7684; next &#x6307;&#x5411;&#x65B0;&#x7ED3;&#x70B9;</span>
    Q.rear = p; <span class="hljs-comment">// &#x65B0;&#x7ED3;&#x70B9;&#x53D8;&#x4E3A;&#x961F;&#x5C3E;&#x7ED3;&#x70B9;</span>
}

<span class="hljs-comment">/**
 * &#x5220;&#x9664;&#x961F;&#x5934;&#x5143;&#x7D20;
 */</span>
<span class="hljs-function">ElemType <span class="hljs-title">deQueue</span><span class="hljs-params">(LinkQueue &amp;Q)</span>
</span>{
    QueuePtr p;
    ElemType e;
    <span class="hljs-keyword">if</span> (Q.front == Q.rear) <span class="hljs-comment">// &#x5934;&#x3001;&#x5C3E;&#x6307;&#x9488;&#x6307;&#x5411;&#x540C;&#x4E00;&#x4E2A;&#x7ED3;&#x70B9;&#xFF0C;&#x8BF4;&#x660E;&#x961F;&#x5217;&#x4E3A;&#x7A7A;</span>
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;ERROR\n&quot;</span>);
        <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
    }
    p = Q.front-&gt;next; <span class="hljs-comment">// &#x7531;&#x4E8E;&#x5B58;&#x5728;&#x5934;&#x7ED3;&#x70B9;&#xFF0C;&#x56E0;&#x6B64;&#x4E0B;&#x4E00;&#x4E2A;&#x7ED3;&#x70B9;&#x624D;&#x662F;&#x961F;&#x5934;&#x7ED3;&#x70B9;</span>
    e = p-&gt;data;
    Q.front-next = p-&gt;next;
    <span class="hljs-keyword">if</span> (Q.rear == p) <span class="hljs-comment">// &#x82E5;&#x961F;&#x5934;&#x7ED3;&#x70B9;&#x5C31;&#x662F;&#x961F;&#x5C3E;&#x7ED3;&#x70B9;&#xFF0C;&#x9700;&#x8981;&#x6539;&#x53D8;&#x5C3E;&#x6307;&#x9488;&#x6307;&#x5411;</span>
    {
        Q.rear = Q.front;
    }
    <span class="hljs-built_in">free</span>(p);
    <span class="hljs-keyword">return</span> e;
}
</code></pre>
<blockquote>
<p>&#x5220;&#x9664;&#x961F;&#x5217;&#x961F;&#x5934;&#x7ED3;&#x70B9;&#x65F6;&#x4EC5;&#x9700;&#x4FEE;&#x6539;&#x5934;&#x7ED3;&#x70B9;&#x7684;&#x6307;&#x9488;&#x5373;&#x53EF;&#xFF0C;&#x4F46;&#x5F53;&#x961F;&#x5217;&#x4E2D;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x88AB;&#x5220;&#x9664;&#x540E;&#xFF0C;&#x961F;&#x5C3E;&#x6307;&#x9488;&#x4E5F;&#x4E22;&#x5931;&#x4E86;&#xFF0C;&#x56E0;&#x6B64;&#x9700;&#x8981;&#x4F7F;&#x961F;&#x5C3E;&#x6307;&#x9488;&#x6307;&#x5411;&#x5934;&#x7ED3;&#x70B9;&#x3002;</p>
</blockquote>
<h4 id="&#x5FAA;&#x73AF;&#x961F;&#x5217;">&#x5FAA;&#x73AF;&#x961F;&#x5217;</h4>
<p><strong>&#x5FAA;&#x73AF;&#x961F;&#x5217;</strong>&#x662F;&#x961F;&#x5217;&#x7684;&#x987A;&#x5E8F;&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#xFF0C;&#x7528;&#x4E00;&#x7EC4;&#x5730;&#x5740;&#x8FDE;&#x7EED;&#x7684;&#x5B58;&#x50A8;&#x5355;&#x5143;&#x4F9D;&#x6B21;&#x5B58;&#x653E;&#x4ECE;&#x961F;&#x5217;&#x5934;&#x5230;&#x961F;&#x5217;&#x5C3E;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x9644;&#x8BBE;&#x4E86;&#x4E24;&#x4E2A;&#x6307;&#x9488; <code>front</code> &#x548C; <code>rear</code> &#x5206;&#x522B;&#x6307;&#x793A;&#x961F;&#x5217;&#x5934;&#x5143;&#x7D20;&#x4E0E;&#x961F;&#x5217;&#x5C3E;&#x5143;&#x7D20;&#xFF0C;&#x5C06;&#x961F;&#x5217;&#x81C6;&#x9020;&#x4E3A;&#x4E00;&#x4E2A;&#x5FAA;&#x73AF;&#x7684;&#x7A7A;&#x95F4;&#x3002;</p>
<p>&#x5728;&#x521D;&#x59CB;&#x5316;&#x5EFA;&#x7ACB;&#x7A7A;&#x961F;&#x5217;&#x65F6;&#xFF0C;&#x4EE4; <code>front = rear = 0</code> &#xFF0C;&#x6BCF;&#x5F53;&#x63D2;&#x5165;&#x65B0;&#x7684;&#x961F;&#x5217;&#x5143;&#x7D20;&#x65F6;&#xFF0C;&#x201C;&#x5C3E;&#x6307;&#x9488;&#x589E; 1&#x201D;&#xFF1B;&#x6BCF;&#x5F53;&#x5220;&#x9664;&#x961F;&#x5217;&#x5934;&#x5143;&#x7D20;&#x65F6;&#xFF0C;&#x201C;&#x5934;&#x6307;&#x9488;&#x589E; 1&#x201C;&#x3002;&#x5728;&#x975E;&#x7A7A;&#x961F;&#x5217;&#x4E2D;&#xFF0C;&#x5934;&#x6307;&#x9488;&#x59CB;&#x7EC8;&#x6307;&#x5411;&#x961F;&#x5217;&#x5934;&#x5143;&#x7D20;&#xFF0C;&#x800C;&#x5C3E;&#x6307;&#x9488;&#x59CB;&#x7EC8;&#x6307;&#x5411;&#x961F;&#x5217;&#x5C3E;&#x5143;&#x7D20;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x4EC5;&#x51ED; <code>front = rear</code> &#x65E0;&#x6CD5;&#x5224;&#x65AD;&#x961F;&#x5217;&#x662F;&#x7A7A;&#x8FD8;&#x662F;&#x6EE1;&#xFF0C;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x7B97;&#x6CD5;&#x4E2D;&#xFF0C;&#x5C11;&#x7528;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x7A7A;&#x95F4;&#xFF0C;&#x7EA6;&#x5B9A;<strong>&#x961F;&#x5217;&#x5934;&#x6307;&#x9488;&#x5728;&#x961F;&#x5217;&#x5C3E;&#x6307;&#x9488;&#x7684;&#x4E0B;&#x4E00;&#x4F4D;&#x7F6E;&#xFF08;&#x6307;&#x73AF;&#x72B6;&#x7684;&#x4E0B;&#x4E00;&#x4F4D;&#x7F6E;&#xFF09;&#x4E0A;</strong>&#x4F5C;&#x4E3A;&#x961F;&#x5217;&#x5448;&#x201C;&#x6EE1;&#x201D;&#x72B6;&#x6001;&#x7684;&#x6807;&#x5FD7;&#x3002;</p>
<pre><code class="lang-c">#define MAXSIZE 10 // &#x961F;&#x5217;&#x6700;&#x5927;&#x957F;&#x5EA6;

/**
 * &#x961F;&#x5217;&#x7684;&#x987A;&#x5E8F;&#x5B58;&#x50A8;&#x7ED3;&#x6784;
 */
typedef struct
{
    ElemType *base; // &#x521D;&#x59CB;&#x5316;&#x7684;&#x52A8;&#x6001;&#x5206;&#x914D;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528;&#x6570;&#x7EC4;&#x8868;&#x793A;
    int front; // &#x5934;&#x6307;&#x9488;
    int rear; // &#x5C3E;&#x6307;&#x9488;
}SqQueue;

/**
 * &#x521D;&#x59CB;&#x5316;&#x6784;&#x9020;&#x4E00;&#x4E2A;&#x7A7A;&#x961F;&#x5217;
 */
void initQueue(SqQueue &amp;Q)
{
    Q.base = (ElemType *) malloc(MAXSIZE * sizeof(ElemType));
    if (!Q.base)
    {
        printf(&quot;ERROR\n&quot;);
        exit(0);
    }
    Q.front = Q.rear = 0;
}

/**
 * &#x8FD4;&#x56DE;&#x961F;&#x5217;&#x5143;&#x7D20;&#x4E2A;&#x6570;&#x5373;&#x961F;&#x5217;&#x957F;&#x5EA6;
 */
int queueLength(SqQueue Q)
{
    return (Q.rear - Q.front + MAXSIZE) % MAXSIZE; // &#x987A;&#x5E8F;&#x961F;&#x5217;&#x957F;&#x5EA6;&#x7684;&#x8BA1;&#x7B97;&#x516C;&#x5F0F;
}

/**
 * &#x63D2;&#x5165;&#x5143;&#x7D20; e &#x5230;&#x961F;&#x5217;&#x5C3E;&#x90E8;
 */
void enQueue(SqQueue &amp;Q, ElemType e)
{
    if ((Q.rear + 1) % MAXSIZE == Q.front) // &#x5224;&#x65AD;&#x961F;&#x5217;&#x662F;&#x5426;&#x6EE1;
    {
        printf(&quot;ERROR\n&quot;);
        return;
    }
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear + 1) % MAXSIZE; // &#x961F;&#x5217;&#x5C3E;&#x6307;&#x9488;&#x5411;&#x540E;&#x5FAA;&#x73AF;&#x79FB;&#x52A8;&#x4E00;&#x4F4D;
}

/**
 * &#x5220;&#x9664;&#x961F;&#x5217;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;
 */
ElemType deQueue(SqQueue &amp;Q)
{
    ElemType e;
    if (Q.front == Q.rear) // &#x5224;&#x65AD;&#x961F;&#x5217;&#x662F;&#x5426;&#x4E3A;&#x7A7A;
    {
        printf(&quot;ERROR\n&quot;);
        exit(0);
    }
    e = Q.base[Q.front];
    Q.front = (Q.front + 1) % MAXSIZE;
    return e;
}
</code></pre>
<blockquote>
<p>&#x5FAA;&#x73AF;&#x961F;&#x5217;&#x6307;&#x9488;&#x5FAA;&#x73AF;&#x540E;&#x79FB;&#x4E00;&#x4F4D;&#xFF1A;<code>Q.X = (Q.X + 1) % SIZE</code></p>
<p>&#x5FAA;&#x73AF;&#x961F;&#x5217;&#x7684;&#x957F;&#x5EA6;&#xFF1A;<code>(Q.rear - Q.front + SIZE) % SIZE</code> </p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="栈.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 抽象数据类型栈的定义和实现">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"抽象数据类型队列的定义和实现","level":"1.4.2","depth":2,"next":{"title":"第四章 串","level":"1.5","depth":1,"ref":"","articles":[{"title":"串类型的定义","level":"1.5.1","depth":2,"path":"chapter4/串类型.md","ref":"chapter4/串类型.md","articles":[]},{"title":"串的表示和实现","level":"1.5.2","depth":2,"path":"chapter4/串的表示和实现.md","ref":"chapter4/串的表示和实现.md","articles":[]},{"title":"串的模式匹配算法","level":"1.5.3","depth":2,"path":"chapter4/串的模式匹配算法.md","ref":"chapter4/串的模式匹配算法.md","articles":[]}]},"previous":{"title":"抽象数据类型栈的定义和实现","level":"1.4.1","depth":2,"path":"chapter3/栈.md","ref":"chapter3/栈.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter3/队列.md","mtime":"2018-08-15T10:29:16.602Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-02T10:05:33.540Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

